package com.tydic.portal.controller;

import cn.hutool.core.util.StrUtil;
import com.tydic.borderer.domain.bo.AgainHistoryByPageBo;
import com.tydic.common.core.domain.R;
import com.tydic.common.core.domain.model.LoginUser;
import com.tydic.common.core.enums.CarNumberBusinessTypeEnum;
import com.tydic.common.core.enums.ProductGroupEnum;
import com.tydic.common.core.enums.ProductSaleStatus;
import com.tydic.common.core.exception.api.ApiException;
import com.tydic.common.log.annotation.Log;
import com.tydic.common.log.enums.BusinessType;
import com.tydic.common.mybatis.core.page.TableDataInfo;
import com.tydic.common.satoken.utils.LoginHelper;
import com.tydic.common.web.domain.bo.DetailBo;
import com.tydic.order.domain.request.PlaceOrderRequest;
import com.tydic.order.service.IOrderService;
import com.tydic.product.domain.Product;
import com.tydic.product.domain.bo.ProductBo;
import com.tydic.product.domain.bo.ProductByPageBo;
import com.tydic.product.domain.bo.ProductGroupBo;
import com.tydic.product.domain.bo.StatusBo;
import com.tydic.product.domain.request.AppendCarNumberRequest;
import com.tydic.product.domain.vo.ProductCategoryVo;
import com.tydic.product.domain.vo.ProductVo;
import com.tydic.product.service.ICarNumberService;
import com.tydic.product.service.IProductService;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/portal/product")

public class ProductController {
    private final ICarNumberService carNumberService;
    private final IProductService productService;
    private final IOrderService orderService;

    @GetMapping("calculate")
    /**
     * 计算税费
     */
    public R calculate(String price, String realPrice) {
        return R.ok(productService.calculate(price, realPrice));
    }

    @Log(title = "发布商品", businessType = BusinessType.UPDATE)
    @PostMapping("publishProduct")
    /**
     * 发布商品
     */
    public R publishProduct(@Validated @RequestBody ProductBo request) {
        productService.publishProduct(request);
        return R.ok();
    }

    @Log(title = "发布商品", businessType = BusinessType.UPDATE)
    @PostMapping("publishGroupProduct")
    /**
     * 发布边民组商品
     */
    public R publishGroupProduct(@Validated @RequestBody ProductGroupBo request) {
        productService.publishGroupProduct(request);
        return R.ok();
    }

    @Log(title = "审核商品", businessType = BusinessType.UPDATE)
    @PostMapping("auditAble")
    /**
     * 审核商品
     */
    public R auditAble(@Validated @RequestBody StatusBo request) {
        productService.auditAble(request);
        return R.ok();
    }

    @Log(title = "上架/下架商品", businessType = BusinessType.UPDATE)
    @PostMapping("saleAble")
    /**
     * 上架/下架商品
     */
    public R saleAble(@Validated @RequestBody StatusBo request) {
        productService.saleAble(request);
        return R.ok();
    }

    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @PostMapping("delete")
    /**
     * 删除商品
     */
    public R delete(@Validated @RequestBody DetailBo request) {
        productService.delete(request);
        return R.ok();
    }

    @Log(title = "商品下单", businessType = BusinessType.UPDATE)
    @PostMapping("placeOrder")
    /**
     * 下单
     */
    public R placeOrder(@Validated @RequestBody PlaceOrderRequest request) {
        return R.ok(orderService.placeOrder(request));
    }

    @GetMapping("/getWeightUnit")
    /**
     * 计量单位
     */
    public R getWeightUnit() {
        return R.ok(Arrays.asList("T", "KG", "G"));
    }

    @GetMapping("/getProductCostMap")
    /**
     * 东兴边民互市贸易进口商品收费标准
     */
    public R getProductCostMap() {
        return R.ok(productService.getProductCostMap());
    }

    @GetMapping("getNeedHandler")
    /**
     * 获取商品审核待办
     */
    public R getNeedHandler() {
        return R.ok(0);
    }

    /**
     * 商品类型（合同类型）
     */
    @GetMapping("/productType")
    public R productType() {
        List<ProductCategoryVo> productCategoryResps = productService.queryProductType();
        return R.ok(productCategoryResps);
    }

    @Log(title = "追加车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/appendCarNumber")
    /**
     * 追加车辆信息
     */
    public R appendCarNumber(@Validated @RequestBody AppendCarNumberRequest appendCarNumberRequest) {
        carNumberService.save(appendCarNumberRequest.getProductId(), CarNumberBusinessTypeEnum.PRODUCT.getType(), appendCarNumberRequest.getCarNumbers());
        return R.ok();
    }

    @Log(title = "创建商品", businessType = BusinessType.INSERT)
    @PostMapping("createGroupProduct")
    /**
     * 创建边民组商品
     */
    public R createGroupProduct(@Validated @RequestBody ProductGroupBo request) {
        Product product = productService.saveGroupProduct(request);
        return R.ok("成功", product.getId());
    }

    /**
     * 编辑边民组商品
     */
    @Log(title = "编辑商品", businessType = BusinessType.UPDATE)
    @PostMapping("editGroupProduct")
    public R editGroupProduct(@Validated @RequestBody ProductGroupBo request) {
        Product product = productService.get(request.getId());
        if (product == null) {
            throw new ApiException("商品不存在");
        }
        if (!StrUtil.equals(product.getSale(), ProductSaleStatus.TO_BE_PUBLISH.getStatus())) {
            throw new ApiException("该商品不是待发布的商品，无法进行编辑");
        }
        productService.saveGroupProduct(request);
        return R.ok();
    }

    @Log(title = "编辑商品", businessType = BusinessType.UPDATE)
    @PostMapping("editProduct")
    /**
     * 编辑边民商品
     */
    public R publishGroupProduct(@Validated @RequestBody ProductBo request) {
        productService.editProduct(request);
        return R.ok();
    }

    @Log(title = "发布商品", businessType = BusinessType.UPDATE)
    @GetMapping("/publishGroupProductById")
    /**
     * 通过边民组ID发布边民组商品
     */
    public R publishGroupProduct(@RequestParam("id") @NotBlank(message = "商品ID") String id) {
        productService.publishGroupProduct(id);
        return R.ok();
    }

    /**
     * 查询商品详情
     */
    @PostMapping("getProductDetail")
    public R<ProductVo> getProductDetail(@Validated @RequestBody DetailBo request) {
        return R.ok(productService.getProduct(request));
    }

    @PostMapping("getChildren")
    /**
     * 查询子商品
     */
    public R<List<ProductVo>> getChildren(@Validated @RequestBody DetailBo request) {
        return R.ok(productService.getChildren(request));
    }

    /**
     * 分页查询商品
     */
    @PostMapping("getProductByPage")
    public R<TableDataInfo<ProductVo>> getProductByPage(@Validated @RequestBody ProductByPageBo request) {
        LoginUser currentUser = LoginHelper.getLoginUser();
        if (!currentUser.isBorderer()) {
            if (currentUser.isBordererAgent()) {
                request.setBorderAgentId(currentUser.getBorderInfoId());
            }
            request.setGroupStatus(ProductGroupEnum.PARENT.getStatus());
        } else {
            request.setBordererInfoId(currentUser.getBorderInfoId());
            request.setGroupStatus(ProductGroupEnum.CHILD.getStatus());
        }
        return R.ok(TableDataInfo.build(productService.findPage(request)));
    }

    @PostMapping("getAgentProductHistory")
    /**
     * 查询代理的子商品
     */
    public R<TableDataInfo<ProductVo>> getAgentProductHistory(@Validated @RequestBody AgainHistoryByPageBo request) {
        return R.ok(TableDataInfo.build(productService.getAgentProductHistory(request)));
    }

}
